package me.javaweb.seata.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.javaweb.seata.user.entity.Test;
import me.javaweb.seata.user.feigen.AccountService;
import me.javaweb.seata.user.mapper.TestMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@AllArgsConstructor
public class TestController {

    private final TestMapper mapper;
    private final HttpServletRequest request;
    private final AccountService accountService;

    @PostMapping
    @GlobalTransactional
    public Object save(@RequestParam(value = "flag", defaultValue = "false", required = false) Boolean flag) {
        Test test = new Test(request.getRemoteAddr());
        mapper.insert(test);
        log.info("user 服务插入完成。");
        accountService.save(flag);
        log.info("account 服务插入完成。");
        return test;
    }

    @GetMapping
    public Map<String, Object> list() {
        List<Test> user = mapper.selectList(Wrappers.<Test>lambdaQuery().orderByDesc(Test::getDate));
        List<Test> account = accountService.list();
        Map<String, Object> objectObjectHashMap = new HashMap<>(2);
        objectObjectHashMap.put("user", user);
        objectObjectHashMap.put("account", account);
        return objectObjectHashMap;
    }
}
